package com.zf.shuashua.ui.home.search;

import android.content.Intent;
import android.text.TextUtils;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.meis.base.mei.adapter.MeiBaseAdapter;
import com.meis.base.mei.base.BaseListFragment;
import com.meis.base.mei.entity.Result;
import com.meis.base.mei.status.ViewState;
import com.meis.base.mei.utils.ParseJsonUtils;
import com.zf.shuashua.R;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.bean.VideoInfo;
import com.zf.shuashua.bean.VideoList;
import com.zf.shuashua.ui.custom.Divider3ItemDecoration;
import com.zf.shuashua.video.VideoPlayActivity;

import java.util.List;

import io.reactivex.Observable;

public class SearchVideoListFragment extends BaseListFragment<VideoInfo> {

    private RecyclerView mRecyclerView;
    private SearchVideoListAdapter mAdapter;
    private String name = "";

    public synchronized void reloadData(String keyWord) {
        name = keyWord;
        loadPage(1);
    }

    @Override
    protected void initView() {
        setState(ViewState.EMPTY);
        super.initView();
    }

    @Override
    protected RecyclerView getRecyclerView() {
        mRecyclerView = getView().findViewById(R.id.recycler);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        mRecyclerView.addItemDecoration(new Divider3ItemDecoration(getActivity()));
        ((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        return mRecyclerView;
    }

    @Override
    protected MeiBaseAdapter<VideoInfo> getAdapter() {
        mAdapter = new SearchVideoListAdapter(R.layout.likevideo_item, null);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            VideoList dataList = new VideoList();
            dataList.setDataList(mAdapter.getData());
            Intent intent = new Intent(getContext(), VideoPlayActivity.class);
            intent.putExtra("position", position);
            intent.putExtra("searchList", dataList);
            startActivity(intent);
        });
        return mAdapter;
    }

    public void insertData(int pos, VideoInfo entity) {
        if (mAdapter != null) {
            setState(ViewState.COMPLETED);
            mAdapter.addData(pos, entity);
            mRecyclerView.smoothScrollToPosition(pos);
        }
    }

    @Override
    protected Observable<Result<List<VideoInfo>>> getListObservable(int pageNo) {
        Observable<String> observable = HttpData.getInstance().searchVideo(pageNo, name);
        return getListData(observable);
    }

    private Observable<Result<List<VideoInfo>>> getListData(Observable<String> observable) {
        final Result<List<VideoInfo>> result = new Result<>();
        return observable.map(s -> {
            if (!TextUtils.isEmpty(s)) {
                try {
                    VideoList videoList = ParseJsonUtils.parseData(s, VideoList.class);
                    result.data = videoList.getDataList();
                } catch (Exception e) {
                }
            }
            return result;
        });
    }

    @Override
    protected boolean loadOnShow() {
        return false;
    }

    @Override
    protected boolean loadForce() {
        return false;
    }

    @Override
    public boolean canLoadMore() {
        return true;
    }

    @Override
    public boolean canPullToRefresh() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_list_search_video;
    }

}
